In Java all Functions need to be defined inside a Class in which case they are called Methods.
Overloaded Methods are Methods declared in the same Class that have the same name but different signature.
They have different: Return Type or different number or Type of Input Parameters.
When during Inheritance Child Class declares Method with the same name and signature as an existing Parent's Method,
then it is said that Parent's Method has been Overridden (replaced) by the Child's Method.
Methods must have the same: Name, Return Type, number and Type of Input Parameters.
Method Syntax
public class Test {
//DECLARE METHOD.
public static String greet(String name, int age) {
return(name + " is " + age + " years old");
}
public static void main(String args[]) {
String result = greet("John", 20); //CALL METHOD.
System.out.print(result); //DISPLAY RESULT.
}
}
Overloaded Methods
public class Test {
//METHOD 1 Has the same name and return type but ONE Input Parameter
static void greet(String name) {
System.out.println("Hello " + name);
}
//METHOD 2 Has the same name and return type but TWO Input Parameters
static void greet(String name, int age) {
System.out.println(name + " is " + age + " years old");
}
public static void main(String args[]) {
greet("John" ); //Call Method 1.
greet("John", 20); //Call Method 2.
}
}